package logic.weapons.projectileWeapons.artillary;

import com.jme.math.Vector3f;

import logic.common.team.Team;
import logic.ships.Ship;
import logic.ships.mothership.MotherShip;
import logic.weapons.WeaponFire;
import logic.weapons.WeaponFireController;

public class ArtillaryController extends WeaponFireController {
	
	public enum ArtillaryMode { ToMid, ToEnemyMS }
	
	private static final long serialVersionUID = 1L;
	
	private final float MIN_DIST = 10f;
	
	private MotherShip enemyMotherShip;
	private Vector3f targetLoc;
	private ArtillaryMode mode;
	
	public ArtillaryController(WeaponFire weaponFire, Ship ship, Vector3f dir) {
		super(weaponFire, ship, dir);
		
		Team team = ship.getTeam();
		enemyMotherShip = team.getGame().getEnemyTeam(team.getFraction()).getMotherShip();
		
		targetLoc = getMSHalfLoc(team);
		mode = ArtillaryMode.ToMid;
	}
	
	public static Vector3f getMSHalfLoc(Team team) {
		Vector3f msDir = team.getEnemyTeam().getMotherShip().getLocalTranslation().subtract(team.getMotherShipSpawn());
		Vector3f msDirhalf = msDir.mult(0.5f);
		return  team.getMotherShipSpawn().add(msDirhalf).add(Vector3f.UNIT_Y.mult(msDirhalf.length()));
	}
	
	@Override
	public void extraUpdate(float time) {
		if(weaponFire.getLocalTranslation().distance(targetLoc) <= MIN_DIST) {
			switch(mode) {
				case ToMid: {
					targetLoc = enemyMotherShip.getLocalTranslation();
					mode = ArtillaryMode.ToEnemyMS;
					break;
				}
			}
		}
		
		rotateTo(targetLoc, time / 2f, false);
	}
}